package com.ymatou.iapi.dguser.testcase;

import com.ymatou.iapi.dguser.parameter.BindBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.dguser.parameter.UserDto;
import com.ymatou.iapi.dguser.service.BindCall;
import com.ymatou.iapi.userservice.parameter.AddressRequestDtoBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.DGThirdPartyBindInfo;
import com.ymttest.database.model.DGWxUnionidBindInfo;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.utils.TestDataManager;

import java.util.List;

import org.junit.*;
import org.junit.experimental.categories.Category;
/**
*用户与微信绑定关系
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Bind{
	private static BindBean bindBean;
	private static BindCall bindCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户与微信绑定关系");
	}
	
	@Before
	public void caseUp() {
		bindBean=new BindBean();
		bindCall=new BindCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(bindCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Bind_001() {
		Logger.start(true,"用户与微信绑定关系");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
			UserDto dgUserInfo = DGUserCallService.getDGUserInfo(user);
			System.out.println(dgUserInfo);
			bindBean.setUserId(user);
			bindBean.setAppSource(62);
			bindBean.setOpenId("oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			bindBean.setUnionId(u.getUnionId());
			bindBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
			bindCall.setData(bindBean);
			bindCall.callService();			
			Logger.verifyEquals("true", bindCall.getString("success"), "验证success");
			dgUserInfo = DGUserCallService.getDGUserInfo(user);
			System.out.println(dgUserInfo);
//			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(bindBean.getOpenId());
//			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
//			Logger.verifyEquals(bindBean.getAppSource(), list.get(0).getAppSource(), "验证AppSource");
//			Logger.verifyEquals(bindBean.getUserId(), list.get(0).getUserId(), "验证UserId");
//			Logger.verifyEquals(bindBean.getUnionId(), list.get(0).getUnionId(), "验证UnionId");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Bind_002() {
		Logger.start(false,"用户与微信绑定关系-openid相同失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
			bindBean.setUserId(user);
			bindBean.setAppSource(u.getAppSource());
			bindBean.setOpenId(u.getOpenId());
			bindBean.setUnionId(u.getUnionId());
			bindCall.setData(bindBean);
			bindCall.callService();			
			Logger.verifyEquals("false", bindCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Bind_003() {
		Logger.start(false,"用户与微信绑定关系不输入openid失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
			bindBean.setUserId(user);
			bindBean.setAppSource(62);
//			bindBean.setOpenId("oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			bindBean.setUnionId(u.getUnionId());
			bindCall.setData(bindBean);
			bindCall.callService();			
			Logger.verifyEquals("false", bindCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Bind_004() {
		Logger.start(false,"用户与微信绑定关系不输入userid失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
//			bindBean.setUserId(user);
			bindBean.setAppSource(62);
			bindBean.setOpenId("oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			bindBean.setUnionId(u.getUnionId());
			bindCall.setData(bindBean);
			bindCall.callService();			
			Logger.verifyEquals("false", bindCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Bind_005() {
		Logger.start(false,"用户与微信绑定关系不输入AppSource失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
			bindBean.setUserId(user);
//			bindBean.setAppSource(62);
			bindBean.setOpenId("oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			bindBean.setUnionId(u.getUnionId());
			bindCall.setData(bindBean);
			bindCall.callService();			
			Logger.verifyEquals("false", bindCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Bind_006() {
		Logger.start(false,"用户与微信绑定关系不输入UnionId成功");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			int user = DGUserCallService.UserCreateCall(u);
			bindBean.setUserId(user);
			bindBean.setAppSource(62);
			bindBean.setOpenId("oid" + TestDataManager.getRandomNum(5) + System.currentTimeMillis() % 1000);
			bindBean.setWxAppName(DGUserCallService.TestWxsqBuyer);
//			bindBean.setUnionId(u.getUnionId());
			bindCall.setData(bindBean);
			bindCall.callService();			
			Logger.verifyEquals("true", bindCall.getString("success"), "验证success");
			List<DGThirdPartyBindInfo> list = new DGUserWapper().selectDGThirdPartyOpenId(bindBean.getOpenId());
			Logger.verifyEquals(1, list.size(), "验证有绑定记录");
			Logger.verifyEquals(bindBean.getAppSource(), list.get(0).getAppSource(), "验证AppSource");
			Logger.verifyEquals(bindBean.getUserId(), list.get(0).getUserId(), "验证UserId");
			Logger.verifyEquals("", list.get(0).getUnionId(), "验证UnionId");
		} catch (Exception e) {
			Logger.fail(e);
		}catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	
}